package Queue;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-06
 * Time:13:47
 * Text：515. 在每个树行中找最大值
 */
public class Text4 {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> t1=new ArrayList<>();
        Queue<TreeNode> queue=new ArrayDeque<>();
        if(root==null){
            return t1;
        }
        queue.add(root);
        while(!queue.isEmpty()){
            Queue<TreeNode> queuel=new ArrayDeque<>();
            int max=Integer.MIN_VALUE;
            for(TreeNode node:queue){
                max = Math.max(max,node.val);
                if(node.left!=null){
                    queuel.add(node.left);
                }
                if(node.right!=null){
                    queuel.add(node.right);
                }
            }
            t1.add(max);
            queue=queuel;
        }
        return t1;
    }
    public static void main(String[] args) {

    }
}
